const server = require('http').createServer(handler);
const io = require('socket.io')(server,{cors:true});
const fs = require('fs');

function handler(request,response) {
    console.log('有一个请求进来了');
    fs.readFile(__dirname+'/index.html',(err, data) => {
    if(err){
        response.writeHead(500);
        return response.end('server error!');
    }
    response.writeHead(200);
    response.end(data);
    })

}

io.on('connection', connect => {

    console.log('one connection is coming in!');
    connect.emit('server-news',{name:'zhangsan',age:18});
    connect.on('client-news', data => { 
        console.log(data); });
    connect.on('disconnect', () => { /* … */ });
});

server.listen(3000,'localhost',() => {
    console.log("server which port is 3000 starts successfully !");
});
